
package wordeditor.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExpandFileMots {

	private static void log(String log) {
		System.out.println(log);
	}
	
	protected static String getDateformatte (Date date) {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");	
	return sdf.format(date) ;	
	}
	
	protected static long getDateLigne(String ligne) {
		long nb = - 1 ;
		if (ligne != null && ligne.startsWith("m")) {
			String arr[] = ligne.split(" ", 2);
			String firstWord = arr[0];
			firstWord = firstWord.replace("m", "");
			//int index = firstWord.indexOf('-');
			if (firstWord.length() >= 8) {
				firstWord = firstWord.substring(0, 8);			
				log(firstWord) ;
				try {
					nb = Long.parseLong(firstWord);
				}
				catch (Exception e) {
					nb = -1 ;
				}
			}
		}
		return nb ;
	}
	


	
	protected static String formateLigne(String ligne, String dateBase, String compteur) {
		return "m" + dateBase + "-" + compteur +  ligne ;
	}	
	
	public static void main (String args[]) {
		expandFile ("D:/dev/ws/WordEditor/res/motslenovo.pppmot") ;
	}
	
	
	//
	public static void expandFile (String nomFichier) {
		System.out.println("toto");		
		BufferedReader in;
		StringBuilder  newfilecontent = new StringBuilder()   ;
	    //int nbLignes = 0 ;
	    long numeroMot ;
	    //long numMaxMot = 0;
	    int compteur = 0 ;
	    String numMaxMotformatte = ""  ;
	    String dateFormattee = getDateformatte(new Date());

    	//numMaxMot = 0;
	    
		try {
			log("lecture de " + nomFichier);
			in = new BufferedReader(new FileReader(nomFichier));
			String inputLine = "" ;
						
			while ((inputLine = in.readLine()) != null) {
				inputLine = inputLine.trim();
				numeroMot = getDateLigne(inputLine);
				if (numeroMot == -1 && ! inputLine.equals("")) {					
					compteur++;
					//numMaxMotformatte = getNumeroFormate(numMaxMot);
					newfilecontent.append("\n" + formateLigne(" " + inputLine, dateFormattee ,String.valueOf(compteur) )) ;
					for (int i = 0 ; i < 5 ; i++) {
						String[] chars = {"a", "b", "c", "d", "e" } ;
						newfilecontent.append("\n" + formateLigne("",  dateFormattee ,String.valueOf(compteur) ) + "-" + chars[i]) ;
					}
				} else {
					newfilecontent.append("\n" + inputLine) ;
				}
				log(numMaxMotformatte + "----------" + inputLine);
			}
			in.close();
			
			//écriture
			try {
        		FileWriter fstream = new FileWriter(nomFichier);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(newfilecontent.toString());
                out.close() ;
            } catch (IOException ex) {
            	System.err.println(ex.toString());
            }			
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
}

